package com.xch.sboot.web.api.user;

import com.xch.sboot.common.constant.RequestHeaders;
import com.xch.sboot.common.response.Result;
import com.xch.sboot.config.login.NoNeedLogin;
import com.xch.sboot.config.login.Session;
import com.xch.sboot.service.user.SignService;
import com.xch.sboot.service.user.dto.SignInDTO;
import com.xch.sboot.service.user.dto.SignUpDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * @author xch
 * 2022/12/14 14:43
 */
@Api(tags = "登录、注册")
@RestController
@RequestMapping("/sign")
public class SignController {

    @Resource
    private SignService signService;

    @ApiOperation("注册")
    @PostMapping("/up")
    @NoNeedLogin
    public Result<Void> signup(@Valid @RequestBody SignUpDTO signUpDTO) {
        signService.signup(signUpDTO);
        return Result.ok("注册成功！");
    }

    @ApiOperation("登录")
    @PostMapping("/in")
    @NoNeedLogin
    public Result<Session> signin(@Valid @RequestBody SignInDTO signInDTO) {
        return Result.newR(signService.signin(signInDTO));
    }

    @ApiOperation("登出")
    @GetMapping("/out")
    public Result<Void> signout(@RequestHeader(name = RequestHeaders.AUTHORIZATION) String token) {
        signService.signout(token);
        return Result.ok("登出成功！");
    }
}
